<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Firefox媒体流测试</title>
    <style>
        @import url('https://fonts.googleapis.com/icon?family=Material+Icons');
        @import url('https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&display=swap');

        body {
            font-family: 'Roboto', sans-serif;
            margin: 0;
            padding: 20px;
            background: #f5f5f5;
        }

        .test-container {
            max-width: 1000px;
            margin: 0 auto;
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }

        .test-section {
            margin-bottom: 30px;
            padding: 20px;
            border: 1px solid #e0e0e0;
            border-radius: 8px;
        }

        .test-button {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
            padding: 12px 24px;
            border: none;
            border-radius: 4px;
            font-size: 0.875rem;
            font-weight: 500;
            text-transform: uppercase;
            letter-spacing: 0.5px;
            cursor: pointer;
            transition: all 0.3s;
            min-height: 36px;
            background: #6200ee;
            color: white;
            margin: 5px;
        }

        .test-button:hover {
            background: #3700b3;
            box-shadow: 0 3px 6px rgba(0,0,0,0.16);
        }

        .test-button:disabled {
            background: #e0e0e0;
            color: #757575;
            cursor: not-allowed;
            opacity: 0.6;
        }

        .material-icon {
            font-family: 'Material Icons';
            font-weight: normal;
            font-style: normal;
            font-size: 24px;
            line-height: 1;
            letter-spacing: normal;
            text-transform: none;
            display: inline-block;
            white-space: nowrap;
            word-wrap: normal;
            direction: ltr;
            vertical-align: middle;
        }

        .video-container {
            position: relative;
            border-radius: 8px;
            overflow: hidden;
            background: #000;
            aspect-ratio: 16/9;
            margin: 10px 0;
        }

        .video-element {
            width: 100%;
            height: 100%;
            object-fit: cover;
            border-radius: 8px;
            -moz-transform: scaleX(-1);
            transform: scaleX(-1);
        }

        .video-placeholder {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 100%;
            color: #757575;
            background: #f5f5f5;
        }

        .result {
            margin-top: 20px;
            padding: 10px;
            background: #e8f5e8;
            border-radius: 4px;
            border-left: 4px solid #4caf50;
        }

        .error {
            background: #ffebee;
            border-left-color: #f44336;
        }

        .warning {
            background: #fff3e0;
            border-left-color: #ff9800;
        }

        .info {
            background: #e3f2fd;
            border-left-color: #2196f3;
        }

        .log {
            background: #fafafa;
            border: 1px solid #e0e0e0;
            border-radius: 4px;
            padding: 10px;
            margin-top: 10px;
            font-family: monospace;
            font-size: 0.875rem;
            max-height: 200px;
            overflow-y: auto;
        }

        @-moz-document url-prefix() {
            .test-button {
                pointer-events: auto;
                background-clip: padding-box;
            }
            
            .video-element {
                object-fit: cover;
                width: 100%;
                height: 100%;
                -moz-transform: scaleX(-1);
                transform: scaleX(-1);
            }
            
            .video-container {
                position: relative;
                overflow: hidden;
                background: #000;
            }
        }
    </style>
</head>
<body>
    <div class="test-container">
        <h1>Firefox媒体流测试</h1>
        <p>专门测试Firefox中的媒体流获取和显示问题</p>
        
        <div class="test-section">
            <h3>测试1: 基本媒体流获取</h3>
            <button class="test-button" onclick="testBasicMediaStream()">
                <span class="material-icon">videocam</span>
                获取音视频流
            </button>
            <button class="test-button" onclick="testAudioOnly()">
                <span class="material-icon">mic</span>
                仅获取音频
            </button>
            <button class="test-button" onclick="testVideoOnly()">
                <span class="material-icon">videocam</span>
                仅获取视频
            </button>
            <div id="result1" class="result" style="display: none;"></div>
        </div>

        <div class="test-section">
            <h3>测试2: 视频显示测试</h3>
            <div class="video-container">
                <video id="testVideo" class="video-element" autoplay muted playsinline></video>
                <div id="videoPlaceholder" class="video-placeholder">
                    <span class="material-icon">videocam_off</span>
                    <p>点击上方按钮获取视频流</p>
                </div>
            </div>
        </div>

        <div class="test-section">
            <h3>调试日志</h3>
            <div id="log" class="log"></div>
        </div>
    </div>

    <script>
        let currentStream = null;

        function log(message) {
            const logElement = document.getElementById('log');
            const timestamp = new Date().toLocaleTimeString();
            logElement.innerHTML += `[${timestamp}] ${message}\n`;
            logElement.scrollTop = logElement.scrollHeight;
            console.log(message);
        }

        function showResult(elementId, message, type = 'success') {
            const element = document.getElementById(elementId);
            element.innerHTML = message;
            element.className = `result ${type}`;
            element.style.display = 'block';
        }

        async function testBasicMediaStream() {
            log('开始测试基本媒体流获取...');
            showResult('result1', '<strong>🔄 正在获取音视频流...</strong>', 'warning');
            
            try {
                if (currentStream) {
                    currentStream.getTracks().forEach(track => track.stop());
                }

                const stream = await navigator.mediaDevices.getUserMedia({ 
                    audio: true, 
                    video: {
                        width: { ideal: 640 },
                        height: { ideal: 480 },
                        facingMode: 'user'
                    }
                });
                
                currentStream = stream;
                
                log(`成功获取媒体流: ${stream.id}`);
                log(`轨道数量: ${stream.getTracks().length}`);
                log(`音频轨道: ${stream.getAudioTracks().length}`);
                log(`视频轨道: ${stream.getVideoTracks().length}`);
                
                const video = document.getElementById('testVideo');
                video.srcObject = stream;
                document.getElementById('videoPlaceholder').style.display = 'none';
                video.style.display = 'block';
                
                showResult('result1', `
                    <strong>✅ 音视频流获取成功！</strong><br>
                    流ID: ${stream.id}<br>
                    轨道数量: ${stream.getTracks().length}<br>
                    音频轨道: ${stream.getAudioTracks().length}<br>
                    视频轨道: ${stream.getVideoTracks().length}
                `);
                
            } catch (error) {
                log(`媒体流获取失败: ${error.name} - ${error.message}`);
                showResult('result1', `
                    <strong>❌ 音视频流获取失败</strong><br>
                    错误类型: ${error.name}<br>
                    错误信息: ${error.message}
                `, 'error');
            }
        }

        async function testAudioOnly() {
            log('开始测试仅音频流获取...');
            showResult('result1', '<strong>🔄 正在获取音频流...</strong>', 'warning');
            
            try {
                if (currentStream) {
                    currentStream.getTracks().forEach(track => track.stop());
                }

                const stream = await navigator.mediaDevices.getUserMedia({ 
                    audio: true, 
                    video: false 
                });
                
                currentStream = stream;
                
                log(`成功获取音频流: ${stream.id}`);
                log(`音频轨道: ${stream.getAudioTracks().length}`);
                
                showResult('result1', `
                    <strong>✅ 音频流获取成功！</strong><br>
                    流ID: ${stream.id}<br>
                    音频轨道: ${stream.getAudioTracks().length}
                `);
                
            } catch (error) {
                log(`音频流获取失败: ${error.name} - ${error.message}`);
                showResult('result1', `
                    <strong>❌ 音频流获取失败</strong><br>
                    错误类型: ${error.name}<br>
                    错误信息: ${error.message}
                `, 'error');
            }
        }

        async function testVideoOnly() {
            log('开始测试仅视频流获取...');
            showResult('result1', '<strong>🔄 正在获取视频流...</strong>', 'warning');
            
            try {
                if (currentStream) {
                    currentStream.getTracks().forEach(track => track.stop());
                }

                const stream = await navigator.mediaDevices.getUserMedia({ 
                    audio: false, 
                    video: {
                        width: { ideal: 640 },
                        height: { ideal: 480 },
                        facingMode: 'user'
                    }
                });
                
                currentStream = stream;
                
                log(`成功获取视频流: ${stream.id}`);
                log(`视频轨道: ${stream.getVideoTracks().length}`);
                
                const video = document.getElementById('testVideo');
                video.srcObject = stream;
                document.getElementById('videoPlaceholder').style.display = 'none';
                video.style.display = 'block';
                
                showResult('result1', `
                    <strong>✅ 视频流获取成功！</strong><br>
                    流ID: ${stream.id}<br>
                    视频轨道: ${stream.getVideoTracks().length}
                `);
                
            } catch (error) {
                log(`视频流获取失败: ${error.name} - ${error.message}`);
                showResult('result1', `
                    <strong>❌ 视频流获取失败</strong><br>
                    错误类型: ${error.name}<br>
                    错误信息: ${error.message}
                `, 'error');
            }
        }

        window.addEventListener('load', function() {
            log('Firefox媒体流测试页面已加载');
            log(`浏览器信息: ${navigator.userAgent}`);
            
            const isFirefox = navigator.userAgent.toLowerCase().includes('firefox');
            if (isFirefox) {
                log('检测到Firefox浏览器');
                document.title += ' - Firefox';
            }
            
            if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
                log('浏览器支持getUserMedia API');
            } else {
                log('浏览器不支持getUserMedia API');
            }
        });

        window.addEventListener('beforeunload', function() {
            if (currentStream) {
                currentStream.getTracks().forEach(track => track.stop());
                log('已清理媒体流资源');
            }
        });
    </script>
</body>
</html> 